<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0 auto;

        }

        #box {

            background: url(https://pic1.zhimg.com/v2-6990cd35207e6c2f495915e74a7edc9c_r.jpg) no-repeat;
            text-align: center;
            background-position: center;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            min-width: 1000px;
            z-index: -10;
            zoom: 1;
            background-size: cover;
            -webkit-background-size: cover;
            -o-background-size: cover;
        }

        #whole {
            text-align: center;
            position: relative;
            top: 200px;
        }

        #form {
            width: 100%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.3);
        }

        #main {
            width: 400px;
            height: 50px;
            border: 2px solid rgba(0, 0, 0, 0.2);
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
            margin-top: 50px;
        }

        #result {
            height: 40px
        }

        #inp {
            border: none;
            width: 227px;
            font-size: 16px;
            background-color: transparent;
        }

        input::-webkit-input-placeholder {
            color: rgba(0, 0, 0, 0.6) !important;
        }

        input:-moz-placeholder {
            color: rgba(0, 0, 0, 0.6) !important;
        }

        input::-moz-placeholder {
            color: rgba(0, 0, 0, 0.6) !important;
        }

        input:-ms-input-placeholder {
            color: rgba(0, 0, 0, 0.6) !important;
        }

        button {
            margin-top: 50px;
            /* Variables */
            --button_radius: 0.75em;
            --button_color: #e8e8e8;
            --button_outline_color: #000000;
            font-size: 17px;
            font-weight: bold;
            border: none;
            border-radius: var(--button_radius);
            background: var(--button_outline_color);
        }

        .button_top {
            display: block;
            box-sizing: border-box;
            border: 2px solid var(--button_outline_color);
            border-radius: var(--button_radius);
            padding: 0.75em 1.5em;
            background: var(--button_color);
            color: var(--button_outline_color);
            transform: translateY(-0.2em);
            transition: transform 0.1s ease;
        }

        button:hover .button_top {
            /* Pull the button upwards when hovered */
            transform: translateY(-0.33em);
        }

        button:active .button_top {
            /* Push the button downwards when pressed */
            transform: translateY(0);
        }
    </style>
</head>

<body>
    <div id="box">
        <div id="form">
            <div id="whole">
                <h2>验证码验证</h2>
                <div id="main">
                    <input type="text" id="inp" />
                    <img src="" alt="" id="img">
                </div>
                <button id="btn">
                    <span class="button_top"> 验证
                    </span>
                </button>
            </div>
        </div>
    </div>

</body>
<script>
    var inp = document.getElementById('inp');
    var img = document.getElementById('img');
    var btn = document.getElementById('btn');

    var name = '';//用来存放请求的验证码图片的名字

    // 接收id对象数组
    var array = [];

    request();

    var vid = "";
    // get请求获取验证码图片
    function request() {
        fetch('/users/token', {
            method: 'get',
        }).then(res => res.json())
            .then((res) => {
                console.log(res)
                vid = res.id;
                name = res.imgname;
                var src = `/users/images/${name}`;
                img.src = src;

                // 根据答案的不同设置不同的问题
                if (!isNaN(res.answer)) {
                    inp.placeholder = '请输入图片中的运算结果'
                } else if (typeof (res.answer) == 'string') {
                    let result = new RegExp("[A-Za-z0-9]");
                    if (result.test(res.answer)) {
                        inp.placeholder = '请输入图片中的验证码'
                    } else {
                        if (res.answer.length == 1) {
                            inp.placeholder = '请输入图片中红色的字体'
                        } else {
                            inp.placeholder = '请输入图片中诗句的下一句'
                        }
                    }
                }
            })
    }

    // 实现点击验证码图片切换不同验证码
    img.onclick = function () {
        inp.value = ''
        request();
    }

    // 验证用户输入的验证码是否正确
    btn.onclick = function () {
        // post用户
        fetch('/users/user', {
            method: 'post',
            body: JSON.stringify({ id: vid, val: inp.value }),
            headers: {
                "Content-Type": "application/json"
            }
        }).then(res => { return res.text() })
            .then(res => alert(res))
            .then((res) => {
                inp.value = '';
                request()
            })
    }
    inp.onfocus = function () {
        inp.style.outline = 'none'
    }

</script>

</html>